<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form
            inline
            size="small"
            :model="tableFrom"
            ref="tableFrom"
            label-width="100px"
          >
            <el-form-item label="商品搜索：">
              <el-input
                v-model="tableFrom.keywords"
                placeholder="请输入商品名称，关键字，商品ID"
                clearable
                class="selWidth"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                icon="ios-search"
                label="default"
                class="mr15"
                size="small"
                @click="seachList"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <el-button type="primary" class="mr10" @click="add" size="small"
          >添加分销商品</el-button
        >
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
      >
        <el-table-column prop="id" label="ID" min-width="50" />
        <el-table-column label="商品图" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image
                style="width: 36px; height: 36px"
                :src="scope.row.goodPic"
                :preview-src-list="[scope.row.goodPic]"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="goodName" label="商品名称" min-width="150" />
        <el-table-column prop="price" label="商品售价" min-width="100" />
        <el-table-column prop="oneReward" label="一级佣金" min-width="100" />
        <el-table-column prop="twoReward" label="二级佣金" min-width="100" />
        <el-table-column
          label="操作"
          min-width="120"
          fixed="right"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="edit(scope.row.id)"
              class="mr10"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="handleDelete(scope.row.id, scope.$index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <creat-store ref="grades"></creat-store>
  </div>
</template>

<script>
import { sroreRewardListApi, sroreRewardDeleteApi } from "@/api/user";
import creatStore from "./creatStore";
export default {
  name: "Grade",
  components: { creatStore },
  data() {
    return {
      listLoading: true,
      tableData: {
        data: [],
        total: 0
      },
      tableFrom: {
        page: 1,
        limit: 20,
        isDel: 0
      }
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    seachList() {
      this.tableFrom.page = 1;
      this.getList();
    },
    add() {
      this.$refs.grades.dialogVisible = true;
      this.$refs.grades.isLockStore = false;
      this.$refs.grades.info(null);
    },
    edit(id) {
      this.$refs.grades.dialogVisible = true;
      this.$refs.grades.info(id);
    },
    // 列表
    getList() {
      this.listLoading = true;
      sroreRewardListApi(this.tableFrom)
        .then(res => {
          this.tableData.data = res.list;
          this.tableData.total = res.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    // 删除
    handleDelete(id) {
      this.$modalSure("确定删除吗？").then(() => {
        sroreRewardDeleteApi({ id: id }).then(() => {
          this.$message.success("删除成功");
          this.getList();
        });
      });
    }
  }
};
</script>

<style scoped lang="scss"></style>
